#include "gui.h"

static lv_obj_t * app_null(lv_obj_t *parent, void *user_data)
{
    (void)user_data;
    (void)parent;
//    creat_mbox(lv_scr_act(),"抱歉, 您的设备暂时不支持该功能");
    return NULL;
}
lv_img_dsc_t bin_pic000;
lv_img_dsc_t bin_pic100;
lv_img_dsc_t bin_pic200;
lv_img_dsc_t bin_pic300;
lv_img_dsc_t bin_pic400;
lv_img_dsc_t bin_pic500;
lv_img_dsc_t bin_pic600;

lv_img_dsc_t bin_pic110;
lv_img_dsc_t bin_pic120;
lv_img_dsc_t bin_pic121;
lv_img_dsc_t bin_pic130;
lv_img_dsc_t bin_pic131;
lv_img_dsc_t bin_pic140;
lv_img_dsc_t bin_pic141;
lv_img_dsc_t bin_pic150;
lv_img_dsc_t bin_pic151;
lv_img_dsc_t bin_pic160;
lv_img_dsc_t bin_pic170;
lv_img_dsc_t bin_pic180;
lv_img_dsc_t bin_pic190;

lv_img_dsc_t bin_pic510;
lv_img_dsc_t bin_pic520;
lv_img_dsc_t bin_pic530;
lv_img_dsc_t bin_pic540;

lv_img_dsc_t bin_pic610;
lv_img_dsc_t bin_pic620;
lv_img_dsc_t bin_pic630;
lv_img_dsc_t bin_pic640;

/***********************************私有变量***********************************/
lv_style_t label2_style;                                                 //字体样式2
lv_style_t label3_style;                                                 //字体样式3
/******************************************************************************/


/* 存储当前APP的obj指针,NULL表示当前在桌面 */
lv_obj_t *current_app_obj_user = NULL;


/* 桌面的ICON信息定义 */
static const icon_item icon_desktop_page1[] =
{
	{&bin_pic100,&bin_pic100, "浪漫", create_music_list},
	{&bin_pic200,&bin_pic200, "温馨", create_music_list},
	{&bin_pic300,&bin_pic300, "会客", create_music_list},
	{&bin_pic400,&bin_pic400, "阅读", create_music_list},
	{&bin_pic500,&bin_pic500, "蓝牙", create_app_bluetooth},
	{&bin_pic600,&bin_pic600, "设置", create_app_set},
};
		
/* 保存icon的对象的链表结构 */
typedef struct icon_obj
{
    lv_obj_t *obj;
    struct icon_obj *next;
}_icon_obj_list;
/* 链表表头定义 */
_icon_obj_list *icon_obj_list = NULL;

lv_obj_t *map_bmp;
lv_obj_t *imgbtn[8];
/***********************************************
*函数名称：lv_gui_start
*功    能：显示界面
*入口参数：无
*返 回 值：无	
*备    注：
************************************************/  
void lv_gui_start(void)
{
	lv_obj_t *logo_pic;
	
	lv_obj_t *scr;
	lv_style_copy(&label_style0,&lv_style_plain_color);
  label_style0.text.font = &myFont20;
	label_style0.text.color = LV_COLOR_MAKE(0x64,0x5C,0xEB);
	
	lv_style_copy(&label2_style,&lv_style_plain_color);
  label2_style.text.font = &lv_font_roboto_16;
	label2_style.text.color = LV_COLOR_MAKE(0x64,0x5C,0xEB);;
	
	lv_style_copy(&label3_style,&lv_style_plain_color);
  label3_style.text.font = &lv_font_roboto_22;
	label3_style.text.color = LV_COLOR_MAKE(0x64,0x5C,0xEB);;
	
	map_bmp = lv_img_create(lv_scr_act(),NULL);                      //背景图片
	lv_img_set_src(map_bmp,&bin_pic000);
	lv_obj_set_protect(map_bmp, LV_PROTECT_POS);
	
	lv_create_icon_desktop(map_bmp,(icon_item*)icon_desktop_page1, sizeof(icon_desktop_page1) / sizeof(icon_desktop_page1[0]), icon_obj_event_cb);                                                 //页面1
}



static void lv_create_icon_desktop(lv_obj_t *parent, icon_item *item, uint16_t icon_num, lv_event_cb_t icon_event_cb)
{
	uint16_t i = 0;
	uint16_t x_pos = 0, y_pos = 0;
	
	for (i = 0; i < icon_num; i++){
		//桌面图标坐标
		x_pos = ((i % 2) * 236) + 10;
		y_pos = ((i / 2) * 120) + 90;
		
		imgbtn[i] = lv_imgbtn_create(parent, NULL);
//		lv_obj_t *label = lv_label_create(parent, NULL);
		
		lv_obj_set_event_cb(imgbtn[i], icon_event_cb);
		icon_obj_list_append(imgbtn[i]);
		
		lv_imgbtn_set_src(imgbtn[i], LV_BTN_STATE_REL,     item[i].icon_img_fc);
		lv_imgbtn_set_src(imgbtn[i], LV_BTN_STATE_PR,      item[i].icon_img_fc);
		lv_imgbtn_set_src(imgbtn[i], LV_BTN_STATE_TGL_REL, item[i].icon_img_bc);
		lv_imgbtn_set_src(imgbtn[i], LV_BTN_STATE_TGL_PR,  item[i].icon_img_bc);
		
		lv_obj_set_pos(imgbtn[i], x_pos, y_pos);
		
//		lv_label_set_style(label, LV_LABEL_STYLE_MAIN, &label_style);
//		lv_label_set_text(label, item[i].text_cn);
//		lv_obj_align(label, imgbtn, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
		
	}
}


static void icon_obj_event_cb(lv_obj_t * obj, lv_event_t event)
{
	uint16_t index = 0;
	uint8_t icon_num_page1;
	_icon_obj_list *p = icon_obj_list;
	if (event == LV_EVENT_RELEASED && (current_app_obj_user == NULL)){
		while (p){
			if (p->obj == obj){
					break;
			}
			index++;
			p = p->next;
		}
		switch_file = index;
		icon_num_page1 = sizeof(icon_desktop_page1) / sizeof(icon_desktop_page1[0]);
		if (index < icon_num_page1){
			/* 第一个页面 */
			//printf("page1\n");
			current_app_obj_user = icon_desktop_page1[index].app(lv_scr_act(),NULL);
		}
	}
}


void icon_obj_list_append(lv_obj_t *obj)
{
	if (icon_obj_list == NULL)
	{
		icon_obj_list = (_icon_obj_list*)lv_mem_alloc(sizeof(_icon_obj_list));
		icon_obj_list->next = NULL;
		icon_obj_list->obj = obj;
	}
	else
	{
		_icon_obj_list *p = icon_obj_list;
		_icon_obj_list *node;
		node = (_icon_obj_list*)lv_mem_alloc(sizeof(_icon_obj_list));
		while (p)
		{
			if (p->next == NULL)
			{
				p->next = node;
				node->next = NULL;
				node->obj = obj;
			}
			p = p->next;
		}
	}
}


